Reuse OpenAI clients to improve throughput - #72
Conversation
- Add module-level singleton clients cached per API key - Replace client creation with _get_sync_client/_get_async_client - Fix chat_history mutation side effect (use list concatenation) - Fix error return type inconsistency in ChatGPT_API_with_finish_reason - Add missing 're' import
|
Nice improvement on client reuse + chat_history immutability. One small concern: _normalize_api_key returns "" when no key is set, so we end up caching a client under the empty-string key. Suggest guarding in _get_sync_client/_get_async_client (raise ValueError if normalized key is empty) or skip caching in that case, to avoid creating a client with an empty API key. |
|
Thanks @luojiyin1987! Since this was opened, the LLM layer was rewritten to route through LiteLLM (#168), so the functions this patches ( Each concern is already covered in the current code:
Closing as superseded by the LiteLLM migration — thanks for the solid fixes, they're all reflected in |
Summary
_get_sync_client/_get_async_clienthelper functionschat_historymutation side effect (use list concatenation instead of append)ChatGPT_API_with_finish_reasonreimportCloses #71